var MawNotificationBar = function(){
	var inited = false;
	var _logoClass = null;
	var _boxWrapper = null;
	var _toggleable = null;
	var _stayDuration = null;
	var _messageDiv = null; 
	var _delayTime = null;
	var _messageIntervalTime = null;
	var _messageList = new Array();
	
	this.startupFlag = false;
	
	this.intervalTime = 2000;
	
	this.doing = false;
	
	this.init = function(opts){
		opts = opts || {};
		if(typeof opts.toggleable !== 'undefined'){
			_toggleable = opts.toggleable;	
		}else{
			_toggleable = true;	
		}
		_logoClass = opts.logoClass || 'mes_logo';
		_stayDuration = opts.stayDuration || 5000;
		_delayTime = opts.delayTime || 0;
		_messageIntervalTime = opts.messageIntervalTime || 10000;
		
		
		if(inited)
			return;
	 
		var a = [];
		 

		a.push('<div class="message_box_hidden">');
		a.push('<div class="message_box">');
		a.push('<a href="#">');
		a.push('<div class="'+_logoClass+'"><span>logo</span></div>');
		a.push('<div class="message">');
		a.push('</div>');
		a.push('</a>'); 
		a.push('</div>');
		a.push('</div>'); 
 
		var context = document.getElementById("jQUi");
		if(!context){
			context = document.body;	
		}
		var tmp = document.createElement("div");
    tmp.innerHTML = a.join("");
    var b = tmp.firstElementChild;
		context.appendChild(b);
		delete tmp;
		 
		
		_boxWrapper = context.querySelector('.message_box_hidden');	
		_boxWrapper.style.webkitTransitionProperty = 'all';
		_boxWrapper.style.webkitTransitionDuration = '1s';
		_boxWrapper.style.webkitTransitionTimingFunction = 'ease-in-out';
		
		_messageDiv = _boxWrapper.querySelector('.message');	
		inited = true;
		
	}
 
	this.startup = function(){
		if(!this.startupFlag){
			setInterval(
				function(){
					if(NotiBar.doing){
						return;	
					} 
					NotiBar.show();
				},
				NotiBar.intervalTime
			);	
		}
		this.startupFlag = true;	
	}
	 
	/**
		@param opts
		{stayDuration:,logoClass:,content:,height:,url:}
		 
	**/
	this.show = function(){
		// invoke from outside ....
		/** 
		if(opts){
			_messageList.push(opts);	
		}
		**/
 
		/**
		var delayTime = (opts && opts.delayTime) || _delayTime;
		console.log("delayTime:"+delayTime);
		if(delayTime > 0){
			setTimeout(function(){doShow();},delayTime);	
		}else{
			doShow();	
		} 
		**/
		
		this.doing = true;
		doShow();
	}
	
	this.addMessage = function(opts){
		_messageList.push(opts);	
	}
	var doShow = function(){ 
		console.log("_messageList.length:"+_messageList.length+" _messageIntervalTime:"+_messageIntervalTime);
		if(_messageList.length > 0){
			var opts = _messageList.shift();
			opts = opts || {};
			opts.height = opts.height || '52px';
	 		
			var stayDuration = opts.stayDuration || _stayDuration; 
			var messageIntervalTime = _messageIntervalTime;
			
			if(opts.logoClass && _logoClass != opts.logoClass){
				var mbox = _boxWrapper.querySelector('.message_box');		
				mbox.firstElementChild.className = opts.logoClass;
				_logoClass = opts.logoClass;
			}
			
			_messageDiv.innerHTML = opts.content || '';
			
			_boxWrapper.style.height = opts.height;
			var linkEle = _messageDiv.parentElement;
			linkEle.href = opts.url;
			
			linkEle.addEventListener("touchstart",function(){
				MawNotificationBar.prototype.hide.call();	
			},false);
			
			var toggleable = (opts.toggleable!=='undefined') ? opts.toggleable : _toggleable; 
			if(toggleable){
				setTimeout(function(){
					MawNotificationBar.prototype.hide.call();	
				},stayDuration);	
			}
			
			if( _messageList.length > 0){
				setTimeout(function(){doShow();},_messageIntervalTime);	
			}else{
				this.doing = false;	
			}
		}else{
			this.doing = false;	
		}
		
	}
	
		
	
	MawNotificationBar.prototype = {
		hide : function(){
			_boxWrapper.style.height = '0px'; 
		},
		
	};
	
	
	
 /**
	(function(intervalTime){
		var __intervalTime = intervalTime;
		//var intervalTime = _intervalTime;
		
		console.log("intervalTime..."+intervalTime);
		 
		setInterval(
			function(){
			 
				if(NotiBar.doing){
					intervalTime = 10000;
					return;	
				}else{
					intervalTime = 	__intervalTime;
				}
				console.log("NotificationBar  intervalTime:"+intervalTime+"  NotiBar.doing: "+NotiBar.doing);
				NotiBar.show();
			},
			intervalTime
		);	
	})(_intervalTime);
	**/
		  
}

var NotiBar = new MawNotificationBar();
 